﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VirtualBank
{
    //大笔金额事件
    public delegate void BigMoneyFetchedEventHandler(object sender, BigMoneyArgs e);
    internal class ATM
    {
        private Bank bank;
        public event BigMoneyFetchedEventHandler BigMoneyFetched;

        public ATM(Bank bank)
        {
            this.bank = bank;
        }

        public ATM() { }

        //取钱
        public void withdrawMoney(int id, double amount)
        {
            Account account = bank.GetAccount(id);
            if (account == null)
                throw new ArgumentException("不合法的账号");

            try
            {
                if (new Random().NextDouble() < 0.3) // 随机概率出现受损的钞票
                    throw new BadCashException("受损的钞票");

                account.withdraw(amount);

                if (amount > 10000)
                {   //金额过大时唤醒大金额事件
                    BigMoneyFetched?.Invoke(this, new BigMoneyArgs(account, amount));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }

    //定义大金额事件
    public class BigMoneyArgs : EventArgs
    {
        public Account Account { get; }
        public double Amount { get; }

        public BigMoneyArgs(Account account, double amount)
        {
            Account = account;
            Amount = amount;
        }
    }
}
